<?php

include_once 'notification/android/AndroidCustomizedcast.php';
include_once 'notification/ios/IOSCustomizedcast.php';

class Umeng
{
    const androidAppkey = '570caaae67e58e431b0001a9';
    const androidAppMasterSecret = 'twkhfmslygjarjc3n8314fw5plo7nxsw';
    const iosAppkey = '5750f59b67e58e2dbf0029bb';
    const iosAppMasterSecret = 'ufdakgu3nb98ygven47fcudasyijdvgp';

    //安卓应用Appkey
    private $androidAppkey;

    //安卓应用AppMasterSecret
    private $androidAppMasterSecret;

    //苹果应用Appkey
    private $iosAppkey;

    //苹果应用AppMasterSecret
    private $iosAppMasterSecret;

    /*
     * 构造方法
     */
    public function __construct()
    {
        $this->androidAppkey = self::androidAppkey;
        $this->androidAppMasterSecret = self::androidAppMasterSecret;
        $this->iosAppkey = self::iosAppkey;
        $this->iosAppMasterSecret = self::iosAppMasterSecret;
    }

    /*
     * android自定义推送
     */
    public function sendAndroidCustomizedcast($title, $content, $alias_type, $alias, $start_time = '', $expire_time = '', $production_mode = true)
    {
        try {
            $customizedcast = new AndroidCustomizedcast();
            $customizedcast->setAppMasterSecret($this->androidAppMasterSecret);
            $customizedcast->setPredefinedKeyValue("appkey", $this->androidAppkey);
            $customizedcast->setPredefinedKeyValue("timestamp", time());
            $customizedcast->setPredefinedKeyValue("alias", $alias);
            $customizedcast->setPredefinedKeyValue("alias_type", $alias_type);
            $customizedcast->setPredefinedKeyValue("display_type", "notification");
            $customizedcast->setPredefinedKeyValue("ticker", "服务消息");
            $customizedcast->setPredefinedKeyValue("title", $title);
            $customizedcast->setPredefinedKeyValue("text", $content);
            $customizedcast->setPredefinedKeyValue("after_open", "go_app");
            $customizedcast->setPredefinedKeyValue("start_time", $start_time);
            $customizedcast->setPredefinedKeyValue("expire_time", $expire_time);
            $customizedcast->setExtraField("kkmtype", 2); //1系统消息 2服务消息
            $customizedcast->setPredefinedKeyValue("production_mode", $production_mode);

            $result = $customizedcast->send();
            return json_decode($result, true);
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /*
     * ios自定义推送
     */
    public function sendIOSCustomizedcast($content, $alias_type, $alias, $start_time = '', $expire_time = '', $production_mode = true)
    {
        try {
            $customizedcast = new IOSCustomizedcast();
            $customizedcast->setAppMasterSecret($this->iosAppMasterSecret);
            $customizedcast->setPredefinedKeyValue("appkey", $this->iosAppkey);
            $customizedcast->setPredefinedKeyValue("timestamp", time());
            $customizedcast->setPredefinedKeyValue("alias", $alias);
            $customizedcast->setPredefinedKeyValue("alias_type", $alias_type);
            $customizedcast->setPredefinedKeyValue("alert", $content);
            $customizedcast->setPredefinedKeyValue("badge", 1);
            $customizedcast->setPredefinedKeyValue("start_time", $start_time);
            $customizedcast->setPredefinedKeyValue("expire_time", $expire_time);
            $customizedcast->setCustomizedField("kkmtype", 2);
            $customizedcast->setPredefinedKeyValue("production_mode", $production_mode);

            $result = $customizedcast->send();
            return json_decode($result, true);
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }
}